#include "StdAfx.h"
#include "SkySphere.h"

SkySphere::SkySphere(float radius) : mSkyShader(sShaderMgr.getShader("SKYSHADER.FX")) {
	if(FAILED(D3DXCreateSphere(sD3D, radius, 100, 100, &mMesh, NULL))) {
		StartupException e;
		e.szTitle = "Failed to load mesh";
		e.szBody = "Unable to load the sphere to display the sky!";
		throw e;
	}
}

void SkySphere::render() {
	auto pos = sCamera.GetCamPosition();
	D3DXMATRIX matTrans, matView, matProj, matViewProj;
	D3DXMatrixTranslation(&matTrans, pos.x, pos.y, pos.z);
	sD3D->GetTransform(D3DTS_VIEW, &matView);
	sD3D->GetTransform(D3DTS_PROJECTION, &matProj);
	
	D3DXMatrixMultiply(&matViewProj, &matTrans, &matView);
	D3DXMatrixMultiply(&matViewProj, &matViewProj, &matProj);
	mSkyShader.setValue("matrixViewProj", matViewProj);

	mSkyShader.doBasicRender([this]()
	{
		mMesh->DrawSubset(0);
	});
}